package cn.wolfcode.wolf2w.controller;



import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;

import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController{

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired//token和redis有关系
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ret=userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        //验证码
        userInfoService.sendVerifyCode(phone);
        //成功
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }


    @PostMapping("/login")
    public Object login(String username, String password) {

        //登录成功只返回用户对象
        UserInfo user=userInfoService.login(username, password);
        //创建token, 然后将token添到redis上
        String token=userInfoRedisService.setToken(user);
        //页面需要: token  user
        //map集合key和value时一对一的
        Map<String, Object> map=new HashMap<>();//在json中的样子{}
        map.put("user", user);
        map.put("token", token);


        /**
         * {
         *     code:xx
         *     msg:xxx
         *     data:{
         *         token
         *         user
         *     }
         * }
         */
        return JsonResult.success(map);
    }

    @RequireLogin//该方法必须登入才能访问
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
        //从请求头获取token
        String token=request.getHeader("token");
        //通过token从redis中获取用户信息
        UserInfo user=userInfoRedisService.getUserByToken(token);
        System.out.println(user);
        return JsonResult.success(user);
    }

//用户攻略id收集集合
    @GetMapping("/strategies/favor")
    public Object strategyFavor(String userId){

        return JsonResult.success(strategyStatisVORedisService.getUserStrategyFavor(userId));


    }


    //访客
    @RequireLogin
    @PostMapping("/totalView")
    public Object totalView(String oId,  UserInfo userInfo){

        strategyStatisVORedisService.visitorNum(oId,1);

        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(oId));
    }

}
